package com.curd.demo.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.curd.demo.common.Constants;
import com.curd.demo.common.Result;
import com.curd.demo.entity.User;
import com.curd.demo.entity.UserDTO;
import com.curd.demo.service.IUserService;
import com.curd.demo.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController//下面所有方法直接返回字符串
//@Controller//视图解析  配合 @RequestBody使用
@RequestMapping("/user")
public class UserController {

//    @RequestBody就不会走视图解析器，直接返回一个字符串//把前端传过来的json对象转成后台的Java对象
    @Resource
    private IUserService userService;
/*********************************   登录与注册  ******************************************/
    //获取登录页面账号密码
//    @PostMapping("/login")
//    public boolean login(@RequestBody UserDTO userDTO) {
//        String username = userDTO.getUsername();
//        String password = userDTO.getPassword();
//        //使用工具类协助判断  判断账号或者密码是否为空
//        if (StrUtil.isBlankIfStr(username) || StrUtil.isBlankIfStr(password)){
//            return false;
//        }
//        return userService.login(userDTO);
//    }
    @PostMapping("/login")
    //              登录接口
    public Result login(@RequestBody UserDTO userDTO) {
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        //使用工具类协助判断  判断账号或者密码是否为空
        if (StrUtil.isBlankIfStr(username) || StrUtil.isBlankIfStr(password)){
            return Result.error(Constants.C0DE_400,"参数错误");
        }
        UserDTO dto = userService.login(userDTO);
        return Result.success(dto);
    }

    @PostMapping("/register")
    //              注册接口
    public Result register(@RequestBody UserDTO userDTO) {
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if (StrUtil.isBlankIfStr(username) || StrUtil.isBlankIfStr(password)){
            return Result.error(Constants.C0DE_400,"参数错误");
        }
        return Result.success(userService.register(userDTO));
    }

    @GetMapping("/username/{username}")
    //    个人信息请求接口
    public Result findOne(@PathVariable String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return Result.success(userService.getOne(queryWrapper));
    }
/************************************************************************/
//          将boolean和其他类型封装成Result  前端所有请求接口的地方都要改造 例如res.data
    @PostMapping
    public Result save(@RequestBody User user) {

        user.setTime(new Date());//获取时间

        return Result.success(userService.saveOrUpdate(user));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(userService.removeById(id));
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(userService.removeByIds(ids));
    }

    @GetMapping
    public Result findAll() {
        return Result.success(userService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(userService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String username,
                               @RequestParam(defaultValue = "") String email,
                               @RequestParam(defaultValue = "") String address) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(username)) {
            queryWrapper.like("username", username);
        }
        if (!"".equals(email)) {
            queryWrapper.like("email", email);
        }
//        if (!"".equals(address)) {
//            queryWrapper.like("address", address);
//        }

        //获取当前用户信息
        User currentUser = TokenUtils.getCurrentUser();
        System.out.println("   当前登录用户为 ======================================" + currentUser.getNickname());

        return Result.success(userService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}
